//
//  LXYearCheckRecordViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/28.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXYearCheckRecordViewController.h"
#import "LXInsuranceTableViewCell.h"
#import "LXYearCheckModel.h"
#import "LXYearCheckApplyViewController.h"
#import "LXCommSearchController.h"
#import "LXYearCheckTableViewCell.h"
@interface LXYearCheckRecordViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) NSInteger pageIndex;
@property (nonatomic) LXCommSearchView *searchView;
@end

@implementation LXYearCheckRecordViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (kUserDefaultForBool(kRefreshStr)) {
        kUserDefaultSetBool(NO, kRefreshStr);
        [self.tableView.mj_header beginRefreshing];
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    if (!ObjectIsEmpty(self.carModel)) {
        UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 70, KStatuesBarHeight, 60, 40)];
       rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
       [rightButton setTitle:@"新增" forState:UIControlStateNormal];
        [rightButton setImage:Image(@"icon_add") forState:UIControlStateNormal];
       [rightButton setTitleColor:mainColor forState:UIControlStateNormal];
        [rightButton addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
       [self.view addSubview:rightButton];
    }
    
    
    if (self.type == 1) {
        [self layoutCustomNav:self.view title:@"年检记录"];
    }
    else {
        [self layoutCustomNav:self.view title:@"年检提醒"];
    }
    
    
    if (self.type==2) {
        LXCommSearchView *searchView = [LXCommSearchView shareInstance];
        searchView.frame = CGRectMake(0, LL_StatusBarAndNavigationBarHeight, kScreenWidth, 60);
        [searchView.searchBtn setTitle:@"请输入车牌号或负责人查询" forState:UIControlStateNormal];
        searchView.type = LXYearCheck_TYPE;
        searchView.url = TyearlyinspectionList_URL;
        self.searchView = searchView;
        [self.view addSubview:searchView];
    }
    
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXInsuranceTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXInsuranceTableViewCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXYearCheckTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXYearCheckTableViewCell class])];

    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        [self loadData];
    }];

    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
         self.pageIndex++;
        [self loadData];
    }];
    

    
}

- (void)loadData{
    
    NSString *url = [DOMAIN_URL stringByAppendingFormat:TyearlyinspectionPageList_URL,@(self.pageIndex),@(10)];
    
    if (!StringIsEmpty(self.carModel.ID)) {
        url = [url stringByAppendingFormat:@"&carId=%@",self.carModel.ID];
    }
    
    
    if (!StringIsEmpty(self.customerId)) {
        url = [url stringByAppendingFormat:@"&customerId=%@",self.customerId];
    }
    // isRemind=1 提醒车辆
    else if (self.type==2) {
        url = [DOMAIN_URL stringByAppendingFormat:TyearlyinspectionList_URL,@(self.pageIndex),@(10)];
        url = [url stringByAppendingString:@"&isRemind=1"];
    }
    
    
    [MSNetworkManager getRevertUrl:url params:@{} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        [self endRefreshing];
        
        if ([self.tableView.mj_header isRefreshing]||self.pageIndex==1) {
            [self.data removeAllObjects];
            [self.tableView.mj_footer resetNoMoreData];
        }
        NSInteger totalPage = [resonseObj[@"total"] integerValue];
            
        NSArray *records = resonseObj[@"records"];
        
        [self.data addObjectsFromArray:[LXYearCheckModel  mj_objectArrayWithKeyValuesArray:records]];
        
        
        if (self.data.count>=totalPage) {
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }
        else {
            self.tableView.mj_footer.state = MJRefreshStateIdle;
        }
        
            
        [self.tableView reloadData];
            
        [self isHaveEmptyData];
            
       
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self endRefreshing];
        [self isHaveEmptyData];
    } withShowHud:YES];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    
    if (self.type == 1) {
        LXYearCheckTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXYearCheckTableViewCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setClipsToBounds:YES];
        LXYearCheckModel *model = self.data[indexPath.row];
        cell.model = model;
        return cell;
    }
    else {
        LXInsuranceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXInsuranceTableViewCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.yearCheckModel = self.data[indexPath.row];
        return cell;
    }
 
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    
    LXYearCheckModel *model = self.data[indexPath.row];

    
    NSDictionary *dic = @{
        @"id":model.yearlyInspectionId,
    };

    [MSNetworkManager getRevertUrl:GET_GetTyearlyinspectionDetail params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
      
        LXYearCheckModel *model = [LXYearCheckModel  mj_objectWithKeyValues:resonseObj];
        LXYearCheckApplyViewController *vc = [[LXYearCheckApplyViewController alloc] init];
        vc.type = 2;
        vc.yearCheckModel = model;
        [self pushToNextController:vc];
            
       
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
      
    } withShowHud:YES];

    
    
}


// 新增年检
-(void)clickBtn{
    LXYearCheckApplyViewController *vc = [[LXYearCheckApplyViewController alloc] init];
    vc.type = 1;
    LXYearCheckModel *yearCheckModel = [LXYearCheckModel new];
    yearCheckModel.carNum = self.carModel.carNum;
    yearCheckModel.carId = self.carModel.ID;
    vc.yearCheckModel = yearCheckModel;
    [self pushToNextController:vc];
    
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, MaxY(self.searchView), ScreenWidth, kScreenHeight - MaxY(self.searchView)) style:UITableViewStylePlain];
        if (self.type==1) {
            _tableView.frame = CGRectMake(0, kViewTopHeight, kScreenWidth, kScreenHeight - kViewTopHeight);
            _tableView.rowHeight = UITableViewAutomaticDimension;

        }
        else {
            _tableView.frame = CGRectMake(0, MaxY(self.searchView), kScreenWidth, kScreenHeight - MaxY(self.searchView));
            _tableView.rowHeight = 95;

        }
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = ColorWithF7F8FA;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

-(NSMutableArray *)data{
    if (!_data) {
        _data = [NSMutableArray array];
    }
    return _data;
}
- (void)isHaveEmptyData{
    if (self.data.count == 0) {
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tipImage = [UIImage imageNamed:@"No_order_icon"];
        self.tableView.tipView.hidden = NO;
        self.tableView.scrollEnabled = YES;
    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}


- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_header.state = MJRefreshStateIdle;
        [self.tableView.mj_footer resetNoMoreData];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}

@end
